Harmonics
Volume Number: 7
Issue Number: 4
Column Tag: C Workshop
Harmonic Simulation
By Byron Miller, Minneapolis, MN
Simulating Reality on The Mac
This article is the end result of my desire to couple simulation techniques and the
Mac’s graphical user interface to produce an application that mimics a physical system.
It was the Mac’s graphical user interface that initially attracted me to the Mac for this
application, because it is an ideal environment for visualizing the simulation results of
“what if” analyses. In this article, the physical system I chose to simulate is damped
harmonic motion.
I chose to simulate damped harmonic motion for two reasons: First is that the
model is well understood -- it is a 2nd order differential equation. Its solution can be
found by solving this equation or by extracting it from any elementary physics
textbook and since the model is predefined, I do not have tackle the model generation
phase (more on this below). Secondly, it is a good example because many physical
systems (like pendulums and planets’ orbits) exhibit this type of harmonic motion.
Simulation Basics
The word simulation means different things to different people. Within the
context of this paper I’ll use the definition used in Systems Simulation: “The Art and
Science:” Simulation is the process of designing a model of a real system and conducting
experiments with this model for the purpose of either understanding the behavior of
the system or evaluating various strategies for the operation of the system.” This is a
nice definition but how can it be applied to produce a model which is simulatable? In
order to fully understand and apply simulation techniques the definition must be
distilled down into its constituents. Simply put, simulation equals model generation
plus experimentation plus verification.
Model generation is the process of describing the relationship of the inputs to the
outputs of a system. This relation is referred to as the transfer function of the system
and it is the actual model; as a side note, the generation phase is usually the most
difficult part of the simulation process and may often seem more like an art than a
science. This type of model generation is often referred to as black box or abstract
modeling because a mathematical relationship is assigned to the box that describes a real
physical system (for example a mass on a spring). There are two different approaches
to this type of model generation: top-down and bottom-up. From a top-down view, the
whole system is completely described by the single mathematical relationship. Whereas
the bottom-up perspective describe a system that is simply a building block for
creation of a larger system.
The model experimentation phase consists simply of observing and recording the
relationship of the input to the output of the system, and its purpose is to prepare for
verification. The model is run with various inputs, and the output from the system is
recorded for the verification phase.
Verification determines whether the model accurately describes the system. If it
does, the model is said to be valid. If not, a new model is generated and retested. The
whole simulation cycle continues until the model is determined to be valid. [What
constitutes a valid model is subjective and depends on the implementor. In other words,
how comfortable is he/she with how closely his/her model represents the real one they
are modeling.] a couple of different approaches may be used for determining validity.
One is “heuristic”, meaning how close the output looks or “feels” like the real one.
Under some conditions the heuristic validation approach may be enough. Other times,
however, this may not be enough and a more formalized method may be needed to
determine how close the model is to representing the real one. For this case, the
statistical approach is used to see how closely the model resembles the real system.
Theory
When a pulling or a pushing force is applied to the system it has a tendency to
return to its initial position with a certain force. This force is actually the sum of two
separate forces which describe the system, the restoring force and the damped force
(figure 1). These two forces completely describe the system. Once these forces are
explicitly defined, the model generation phase is complete. The forces describing the
system are:
F = damping force + restoring force = - (b * (dx/dt) + kx)
where F is the total force of the system, b is the coefficient of friction, dx/dt is
the velocity in the x direction, k is the elastic coefficient, x is the displacement, and
the minus sign denotes that the sum of these forces is of equal and opposite magnitude of
the external force applied to the system. Without going into all the mathematics, the
solution to this equation is:
x = (A * e-b * t/2 * m) * cos(w * t),
where w (omega) equals (1 / 2 * m ) * (4 * M * k - b^2)^0.5, and A is the initial
displacement when an external force is released.
Translated the solution means that the output of the system “moves” (produces
positive and negative values like a wave) back and forth with a maximum distance of A
and with a frequency of omega (w).
Varying the values of b, k and m causes the system to behave differently. If b^2 <
4 * M * k the system oscillates indefinitely. On the other hand, if b^2 > 4 * M * k, the
system oscillates for a while but eventually come to rest. If b^2 = 4 * M * k, the
system immediately comes to rest. These three cases are referred to as underdamping,
overdamping, and critical damping respectively.
Implementation
The program consists of three logical sections: computational, plot and status.
The program itself is loosely layed out in a similar fashion; and each section is
described in terms of functionality from the top level function that performs the task.
The computational section performs all of the mathematical crunching to simulate the
model. The plot section graphically displays the results, and the status section provides
non-intuitive information on the simulation. Each section has a corresponding top level
function which I shall describe briefly below.
Procedure Calculate Harmonics iteratively computes values from the
mathematical model and then saves the results to a simulation result file. Calculate
Harmonics begins by dynamically allocating enough storage for data (dynamic allocation
is used over static storage methods because it allows the model to be bounded only by
the amount of available ram). Next Calculate Harmonics arranges its input in the
Standard Apple Numeric Environment (SANE) floating point format. The input is then
converted into doubles for maintaining accuracy. After computation only the positive
values are retained and coerced into an integer format. The integer values are then
converted to the American Standard for Information Interchange (ASCII) format and
stored in the simulation result file. The simulation result file contains the x and y
labels in the first two lines followed by some number of x and y pairs delimited by a
carriage return.
The plotting section is handled by the MainPlot function. MainPlot plots only the
positive values of the solution. Input to MainPlot comes from the file generated by
Calculate Harmonics. The plot function is accomplished by first creating the axes,
gradients and then labeling the axes. Next the x and y coordinates are read in and
converted from the file and space is allocated for them (also dynamically). The
converted data is then normalized and transformed to map over into the quickdraw local
coordinate system.
Status maintenance is handled by the top level function doStatus. DoStatus either
stores information about the last simulation run, or displays the information of the last
simulation. The function computes w (omega) and determines whether the simulation
is over, under, or critically damped.
I should mention that I have included a handful of simple functions that already
exist in the standard library. If you like, you may use these. I chose not to use them
because including the library to the project adds about 10k to the executable program.
What’s Next?
You could improve the program or produce your own simulation. The program
could be improved in terms of performance and functionality. For instance, updating
could be accelerated by plotting the graph initially as a pict instead of redrawing it
every time as it currently is written. Overall conversion time could be reduced by
storing the data points in a binary format instead of in ASCII. From a feature standpoint
the plotting could be expandable to support both positive and negative values. An
ability to zoom the plot window and store the plot to a file or dump the plot to the
printer would also be a nice feature. As is, this application serves as a teaching tool: ie,
it provides an example of how a process is transformed to its mathematical equivalence.
The same techniques used to produce the HSIM application are applicable to other
phenomenon and may be used to create simulations of your own. Remember the great
thing about simulation is that it can be both useful and fun. Unlike the real world, the
world of simulation is limited only by your imagination..., enjoy!
Bibliography
Halliday, D. and Resnick, R. Physics Parts 1 & 2, 3rd Edition:John Wiley and
Sons, 1978.
Payne, J. A. Introduction to Simulation: Programming Techniques and Methods of
Analysis. Mc Graw - Hill Inc., 1982.
Shannon, R. E. System Simulation: The Art and Science. Englewood Cliffs, NJ:
Prentice - Hall Inc., 1975.
Listing: Hanrmonic.h
#define FALSE 0
#define TRUE 1
#define BASE_RES_ID 400
#define NIL 0L
#define MOVE_TO_FRONT -1L
#define REMOVE_ALL_EVENTS 0
#define SLEEP 0L
#define NIL_MOUSE_REGION 0L
#define WNE_TRAP_NUM 0x60
#define UNIMPL_TRAP_NUM 0x9f
#define BANNER_OFFSET 96
#define HORIZONTAL_OFFSET 0
#define INITIAL_ROW 24
#define LEFT_MARGIN 10
#define ROW_HEIGHT 15
#define START_ROW 0
#define TEXT_FONT_SIZE 10 /* in pixels */
#define GRAD_FONT_SIZE 6
#define APPLE_MENU_ID BASE_RES_ID
#define FILE_MENU_ID BASE_RES_ID+1
#define EDIT_MENU_ID BASE_RES_ID+2
#define OPTION_MENU_ID BASE_RES_ID+3
#define TRANSFER_ITEM 1
#define QUIT_ITEM 3
#define ABOUT_ITEM 1
#define COPY_ITEM 4
#define PASTE_ITEM 5
#define CLEAR_ITEM 6
#define CLIPBOARD_ITEM 7
#define SIMULATE_ITEM 1
#define PLOT_ITEM 2
#define STATUS_ITEM 3